﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace JuCloudy.Sys.App.AppInfo
{
    public partial class MainPage : UserControl
        ,JuCloudy.Framework.Interface.IModule<JuCloudy.Data.Entity.SysAppInfo>
    {
        public MainPage()
        {
            InitializeComponent();
            this.Loaded += (obj, e) =>
            {
                OnLoad(); InitData();
            };
        }
        
        private ObservableCollection<JuCloudy.Data.Entity.SysAppInfo> entities
        {
            get { return new ObservableCollection<Data.Entity.SysAppInfo>((IEnumerable<Data.Entity.SysAppInfo>)this._dg.ItemsSource); }
            set { this._dg.ItemsSource = value; }
        }

        private ObservableCollection<JuCloudy.Data.Entity.SysCategory> Categories { get; set; }

        private void InitData()
        {
            try
            {
                this.entities = JuCloudy.Framework.Storge.VariableHelper.ItemSource
                    as ObservableCollection<JuCloudy.Data.Entity.SysAppInfo>;
            }
            catch (Exception ex) { MessageBox.Show(ex.ToString()); }
        }

        public void insert(JuCloudy.Data.Entity.SysAppInfo entity)
        {
            JuCloudy.Framework.Storge.VariableHelper.Structure.ShowLoading();
            SystemService.WSSysServiceSoapClient client
                = new SystemService.WSSysServiceSoapClient();
            client.InsertAppInfoAsync(entity.Serialize());
            client.InsertAppInfoCompleted += (obj, ex) =>
            {
                JuCloudy.Framework.Storge.VariableHelper.Structure.CloseLoading();
                if (ex.Error != null)
                    throw ex.Error;

                this.entities = ex.Result.Deserialize(typeof(ObservableCollection<JuCloudy.Data.Entity.SysAppInfo>))
                    as ObservableCollection<JuCloudy.Data.Entity.SysAppInfo>;
                JuCloudy.Framework.Storge.VariableHelper.Structure.CloseChildDialog();
            };
        }

        public void delete(JuCloudy.Data.Entity.SysAppInfo entity)
        {
            JuCloudy.Framework.Storge.VariableHelper.Structure.ShowLoading();
            SystemService.WSSysServiceSoapClient client
                = new SystemService.WSSysServiceSoapClient();
            client.DeleteAppInfoAsync(entity.Serialize());
            client.DeleteAppInfoCompleted += (obj, ex) =>
            {
                JuCloudy.Framework.Storge.VariableHelper.Structure.CloseLoading();
                if (ex.Error != null)
                    throw ex.Error;

                this.entities = ex.Result.Deserialize(typeof(ObservableCollection<JuCloudy.Data.Entity.SysAppInfo>))
                    as ObservableCollection<JuCloudy.Data.Entity.SysAppInfo>;
                JuCloudy.Framework.Storge.VariableHelper.Structure.CloseChildDialog();
            };
        }

        private void OnLoad()
        {
            this._insertbtn.Click += (obj, e) =>
            {
                controls.insert insert = new controls.insert();
                insert.MainPage = this;
                insert.Categories = Categories;
                JuCloudy.Framework.Storge.VariableHelper.Structure.ShowChildDialog(insert, "新增应用");
            };

            this._updatabtn.Click += (obj, e) =>
            {
                controls.insert insert = new controls.insert();
                insert.MainPage = this;
                insert.Categories = Categories;
                insert.Entity = _dg.SelectedItem as JuCloudy.Data.Entity.SysAppInfo;
                JuCloudy.Framework.Storge.VariableHelper.Structure.ShowChildDialog(insert, "编辑应用");
            };

            this._uploadbtn.Click += (obj, e) =>
            {
                controls.upload upload = new controls.upload();
                upload.AppInfo = this._dg.SelectedItem as JuCloudy.Data.Entity.SysAppInfo;
                JuCloudy.Framework.Storge.VariableHelper.Structure.ShowChildDialog(upload, "上传/更新应用包");
            };
        }

        private void _dg_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems != null && e.AddedItems.Count > 0)
            {
                this._updatabtn.IsEnabled = true;
                this._deletebtn.IsEnabled = true;
                this._uploadbtn.IsEnabled = true;
            }
            else
            {
                this._updatabtn.IsEnabled = false;
                this._deletebtn.IsEnabled = false;
                this._uploadbtn.IsEnabled = false;
            }
        }

        public void refresh()
        {
            JuCloudy.Framework.Storge.VariableHelper.Structure.ShowLoading();
            SystemService.WSSysServiceSoapClient client
                = new SystemService.WSSysServiceSoapClient();
            client.GetAppInfoEntitiesAsync();
            client.GetAppInfoEntitiesCompleted += (obj, ex) =>
            {
                if (ex.Error != null)
                    throw ex.Error;

                this.entities = ex.Result.Deserialize(typeof(ObservableCollection<JuCloudy.Data.Entity.SysAppInfo>))
                    as ObservableCollection<JuCloudy.Data.Entity.SysAppInfo>;

                client.GetCategoryEntitiesAsync();
                client.GetCategoryEntitiesCompleted += (objx, exx) =>
                {
                    JuCloudy.Framework.Storge.VariableHelper.Structure.CloseLoading();
                    if (exx.Error != null)
                        throw exx.Error;

                    this.Categories = exx.Result.Deserialize(typeof(ObservableCollection<JuCloudy.Data.Entity.SysCategory>))
                        as ObservableCollection<JuCloudy.Data.Entity.SysCategory>;

                    JuCloudy.Framework.Storge.VariableHelper.Structure.CloseLoading();
                };
            };
        }

        public void InitResource()
        {
            SystemService.WSSysServiceSoapClient client
                = new SystemService.WSSysServiceSoapClient();
            client.GetAppInfoEntitiesAsync();
            client.GetAppInfoEntitiesCompleted += (obj, ex) =>
            {
                if (ex.Error != null)
                    throw ex.Error;

                JuCloudy.Framework.Storge.VariableHelper.ItemSource = ex.Result.Deserialize(typeof(ObservableCollection<JuCloudy.Data.Entity.SysAppInfo>))
                    as ObservableCollection<JuCloudy.Data.Entity.SysAppInfo>;

                client.GetCategoryEntitiesAsync();
                client.GetCategoryEntitiesCompleted += (objx, exx) =>
                {
                    JuCloudy.Framework.Storge.VariableHelper.Structure.CloseLoading();
                    if (exx.Error != null)
                        throw exx.Error;

                    this.Categories = exx.Result.Deserialize(typeof(ObservableCollection<JuCloudy.Data.Entity.SysCategory>))
                        as ObservableCollection<JuCloudy.Data.Entity.SysCategory>;

                    this.Loaded += (objxx, exxx) =>
                    {
                        InitData();
                    };
                };
            };
        }
    }
}
